package com.suxin.websocket.handler;

import com.alibaba.fastjson.JSON;
import com.suxin.websocket.model.BaseModel;
import com.suxin.websocket.model.HeartbeatModel;
import com.suxin.websocket.model.ModelEnum;
import com.suxin.websocket.pusher.HeartbeatPusher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Tang
 * @classname HeartbeatHandler
 * @description [ 心跳 ]
 * @date 2022/3/24 14:57
 */
@Component
@Slf4j
public class HeartbeatHandler implements Handler<BaseModel> {

    @Resource
    HeartbeatPusher heartbeatPusher;

    @Override
    public void handler(BaseModel data) {
        log.info("这是一个心跳包:{}", JSON.toJSONString(data));
        HeartbeatModel heartbeatModel = new HeartbeatModel();
        heartbeatModel.setMsg("PONG");
        heartbeatModel.setKey(data.getKey());
        heartbeatPusher.push(heartbeatModel);
    }

    @Override
    public boolean isSupport(BaseModel date) {
        return date.getType() == ModelEnum.HEARTBEAT.getCode();
    }
}